package servlet;

import com.alibaba.fastjson.JSON;
import until.BaseDao;
import until.BaseModel;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

@WebServlet(name = "UpdataUserServlet",urlPatterns = "/updatauser")
public class UpdataUserServlet extends HttpServlet {
    BaseDao baseDao = new BaseDao();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        Object[] params = new Object[12];
        params[0]=req.getParameter("name");
        params[1]=req.getParameter("trueName");
        params[2]=req.getParameter("cardId");
        params[3]=req.getParameter("email");
        params[4]=req.getParameter("telephone");
        params[5]=req.getParameter("qqId");
        params[6]=req.getParameter("pwd");
        params[7]=new Date();
        params[8]=req.getParameter("question");
        params[9]=req.getParameter("answer");
        params[10]=req.getParameter("state");
        params[11]=req.getParameter("id");
        String sql="update t_user set name=?,trueName=?,cardId=?,email=?,telephone=?,qqId=?,pwd=?,regTime=?,question=?,answer=?,state=? where id=?";
        int result=baseDao.update(sql,params);
        BaseModel baseModel = new BaseModel();
        if (result>0){
            baseModel.setCode(1);
            baseModel.setMessage("修改用户信息成功！");
        }else{
            baseModel.setCode(0);
            baseModel.setMessage("修改用户信息失败!");
        }
        String baseModelStr=JSON.toJSONString(baseModel);
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write(baseModelStr);
        resp.getWriter().flush();
        resp.getWriter().close();
    }
}
